package diona;

import com.melloware.jintellitype.JIntellitype;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * User: anton
 * Date: 16.03.2010
 */
public class HotKeyListenerApplication
{
    private static Logger LOG = LoggerFactory.getLogger(HotKeyListenerApplication.class);

    private volatile TrayIcon trayIcon;
    private final DbSchemeChecker schemeChecker;
    private final HotKeyListenerFactory listenerFactory;

    public HotKeyListenerApplication(DbSchemeChecker schemeChecker, HotKeyListenerFactory listenerFactory)
    {
        this.schemeChecker = schemeChecker;
        this.listenerFactory = listenerFactory;
    }

    public void start()
    {
        schemeChecker.initDb();
        LOG.debug("Checking whether jintellitype supported");

        if (!JIntellitype.isJIntellitypeSupported()) {
            LOG.error("jintellitype is not supported");
            throw new IllegalStateException("jintellitype is not supported");
        }

        LOG.debug("jintellitype is supported");

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            LOG.warn("Cannot set l&f", e);
        }
        try {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    trayIcon = createAndShowGUI();
                }
            });
        } catch (Exception e) {
            LOG.error("Cannot create tray item", e);
        }
        listenerFactory.initHotKeysListeners(trayIcon);
    }

    private static TrayIcon createAndShowGUI()
    {
        if (!SystemTray.isSupported()) {
            LOG.warn("SystemTray is not supported");
            return null;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon = new TrayIcon(createImage("icon.gif", "tray icon"));
        trayIcon.setImageAutoSize(true);
        final SystemTray tray = SystemTray.getSystemTray();

        MenuItem exitItem = new MenuItem("Exit");

        popup.add(exitItem);

        trayIcon.setPopupMenu(popup);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            LOG.error("TrayIcon could not be added", e);
            return null;
        }

        exitItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                LOG.debug("Trying to shutdown application...");
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
        return trayIcon;
    }

    private static Image createImage(String path, String description)
    {
        URL imageURL = HotKeyListenerApplication.class.getResource(path);

        if (imageURL == null) {
            LOG.error("Resource not found: {}", path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }


}
